package com.macro.mall.portal.service.impl;

import com.easemob.im.api.ContactApi;
import com.easemob.im.api.UserApi;
import com.easemob.im.api.model.EMCreateUser;
import com.easemob.im.api.model.EMCreateUsersResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class IMUtil {

    @Autowired
    private UserApi userApi;

    @Autowired
    private ContactApi contactApi;



    public  void importUser(String username) {
        List<EMCreateUser> emCreateUserList = new ArrayList<>();
        EMCreateUser createUser = new EMCreateUser();
        createUser.setUsername(username);
        createUser.setPassword("123456");
        emCreateUserList.add(createUser);
        try {
            EMCreateUsersResult result = userApi.createUsers(emCreateUserList);
            System.out.println(result);
        } catch (Exception e) {
            e.getMessage();
        }
    }

    /**
     * 添加好友
     * @param username  发起添加好友的用户
     * @param friendUsername 要添加的好友
     */
    public void addFriend(String username, String friendUsername) {
        try {
            // 调用环信添加好友接口
            contactApi.addContact(username, friendUsername);
            System.out.println("用户 " + username + " 成功添加好友：" + friendUsername);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("添加好友失败：" + e.getMessage());
        }
    }

}
